package com.lagou.niancheng.core.config;

import com.lagou.niancheng.core.utils.ReflectionUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 结果元数据
 *
 * @author 大犬
 * @date 2020/3/28
 */
public class ResultTypeMetaData {

    private Class resultType;

    private Map<String, Field> fieldMap;

    public ResultTypeMetaData(Class resultType) {
        this.resultType = resultType;
        initFields();
    }

    private void initFields() {
        Type genericSuperclass = resultType.getGenericSuperclass();

        List<Field> allFields = ReflectionUtil.getAllFields(resultType);
        fieldMap = new HashMap<>(allFields.size());
        allFields.forEach(field -> {
            field.setAccessible(true);
            fieldMap.put(field.getName().toLowerCase(), field);
        });
    }

    public Map<String, Field> getFieldMap() {
        return fieldMap;
    }

    public void setFieldMap(Map<String, Field> fieldMap) {
        this.fieldMap = fieldMap;
    }

    public Class getResultType() {
        return resultType;
    }

    public void setResultType(Class resultType) {
        this.resultType = resultType;
    }


}
